/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.property;

import java.util.ArrayList;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.PropertyDefinition;

import net.phoenix.repository.BasicNode;
import net.phoenix.repository.JCRUtils;
import net.phoenix.repository.Name;

/**
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-5-20
 * @version 1.0.0
 */
public class NodeTypeProperty extends NameProperty {
	private NodeType[] types;
	private Name name;

	/**
	 * create single value property
	 * 
	 * @param node
	 * @param definition
	 * @param value
	 */
	public NodeTypeProperty(BasicNode node, PropertyDefinition definition, Value value) {
		super(node, definition, value);

	}

	/**
	 * create multiple values property
	 * 
	 * @param node
	 * @param definition
	 * @param values
	 */
	public NodeTypeProperty(BasicNode node, PropertyDefinition definition, Value[] values) {
		super(node, definition, values);
	}

	/**
	 * create a property with default value;
	 * 
	 * @param node
	 * @param definition
	 */
	public NodeTypeProperty(BasicNode node, PropertyDefinition definition) {
		super(node, definition);

	}
	

	public PropertyDefinition getDefinition() {
		NodeType base;
		try {
			base = node.getPrimaryNodeType();
		} catch (RepositoryException e) {
			return null;
		}
		return JCRUtils.getPropertyDefinition(base, name.getQName());

	}

	@Override
	public int getType() {
		return PropertyType.NAME;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.BasicProperty#getName()
	 */
	@Override
	public String getName() {
		return this.name.getQName();
	}

	public NodeType[] getNodeTypes() throws RepositoryException {
		NodeTypeManager manager = this.node.getSession().getWorkspace().getNodeTypeManager();
		ArrayList<NodeType> types = new ArrayList<NodeType>();
		if (this.types == null) {
			for (int i = 0; i < values.length; i++) {
				NodeType type = manager.getNodeType(values[i].getString());
				if (type != null)
					types.add(type);
			}
			this.types = types.toArray(new NodeType[types.size()]);
		}
		return this.types;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.phoenix.repository.property.BasicProperty#setValue(java.lang.String)
	 */
	@Override
	public void setValue(String value) throws RepositoryException {
		NodeTypeManager manager = this.node.getSession().getWorkspace().getNodeTypeManager();
		NodeType type = manager.getNodeType(value);
		this.types = new NodeType[] { type };
		super.setValue(value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.phoenix.repository.property.BasicProperty#setValue(java.lang.String
	 * [])
	 */
	@Override
	public void setValue(String[] values) throws RepositoryException {
		NodeTypeManager manager = this.node.getSession().getWorkspace().getNodeTypeManager();
		ArrayList<NodeType> types = new ArrayList<NodeType>();
		for (int i = 0; i < values.length; i++) {
			NodeType type = manager.getNodeType(values[i]);
			if (type != null)
				types.add(type);
		}
		this.types = types.toArray(new NodeType[types.size()]);
		super.setValue(values);

	}
}
